<?php
/**
 *
 * 门锁管理控制器
 * @author comfiger<liuqiang@wisdudu.com>
 * @version v1.0.0
 *
 **/
namespace Mama\Controller;
use Think\Controller;

class LockController extends Controller {
	protected $shopidlist = ""; //管理的分店ID集合
	public function __construct() {
        parent::__construct();
		//判断是否登陆
		if(!session('userid')){
			$this->redirect("index/index");
		}
		//解析分店信息
		$shoplist = S('shoplist_'. session('userid'));
		$this->assign('shoplist', $shoplist);
		if(count($shoplist)) foreach($shoplist as $sp){
			$this->shopidlist .= strlen($this->shopidlist) ? ','. $sp['shop_id'] : $sp['shop_id'];
		}
		//传递参数到分页界面
		$this->assign('param', json_encode(I('get.')));
    }

    /**
     * 门锁列表
     **/
    public function index(){
    	$lockDao = D('Lock');
    	if(IS_GET){
	    	$ajax = I('get.ajax', 0);
			$page = I('get.page', 1);
			//获取姓名
			$locktype = I('get.locktype', '');
			if(!empty($locktype)){
				$where['llock.lock_type'] = $locktype;
			}
			//房间名称
			$doornumber = I('get.doornumber', '');
			if(!empty($doornumber)){
				$where['llock.doornumber'] = $doornumber;
			}
			//获取分店
			$shop_id = I('get.shop_id', 0);
			if(!empty($shop_id)){
				$where['llock.shop_id'] = $shop_id;
			}else{
				$where['llock.shop_id'] = array('in', $this->shopidlist);
			}
			//获取小区条件
			$villageid = I('get.villageid', 0);
			if(!empty($villageid)){
				$where['llock.villageid'] = $villageid;
			}

			//获取小区列表
			$villagelist = D('Village')->getVillageListByShopId(array(
				'shop_id' => array('in', $this->shopidlist)
			));
			$this->assign('villagelist', $villagelist);
			//获取总数
			
			$count = $lockDao->getLockListCount($where);
			
			$this->assign('count', $count);
			//获取列表
			$locklist = $lockDao->getLockList($where, $page);
			$this->assign('locklist', $locklist);
			//获取门锁类型
			$locktypelist = $lockDao->getLockTypeList();
			$this->assign('locktype', $locktypelist);
			$this->assign('page', $page);
			if($ajax){
				$this->display('lock_list', 'utf-8', 'html');
			}else{
				$this->display();
			}
		}else if(IS_POST){
			$lockid = I('post.lockid', 0);
			$row = $lockDao->deleteLockInfoByLockId($lockid);
			if($row !== false){
				echo 1;
			}else{
				echo 0;
			}
		}
    }

    public function lockadd(){
    	$lockDao = D('Lock');
    	if(IS_GET){
    		$locktypelist = $lockDao->getLockTypeList();
			$this->assign('locktype', $locktypelist);
			$lockid = I('get.lockid', 0);
			if($lockid){
				$lockinfo = $lockDao->getLockInfoByLockId($lockid);
				$this->assign('lockinfo', $lockinfo);
				//获取小区列表
				$villagelist = D('Village')->getVillageListByShopId(array(
					'shop_id' => $lockinfo['shop_id']
				));
				$this->assign('villagelist', $villagelist);
			}
    		$this->display('Lock/lock_add');
    	}else{
    		$data = I('post.');
    		$data['admin_id'] = session('userid');
    		$data['admin_nick'] = session('nickname');
    		$data['lock_createtime'] = time();
    		$data['roomid'] = 0;
    		$lockid = $data['lockid'];
    		if(!empty($data['doornumber'])){
	    		//根据小区ID+房号查询房间ID
	    		$roominfo = D('Room')->getRoomInfoByWhere(array(
	    			'doornumber' => $data['doornumber'],
	    			'villageid' => $data['villageid']
	    		));
	    		if(is_array($roominfo) && count($roominfo)){
	    			$data['roomid'] = $roominfo['roomid'];
	    		}else{
	    			echo 2;
	    			exit;
	    		}
    		}else{
    			$data['roomid'] = 0;
	    		$data['doornumber'] = '';
    		}
    		unset($data['lockid']);
    		if($lockid){
    			$row = $lockDao->editLockInfoByLockId($lockid, $data);
    		}else{
	    		//判断是否编辑锁
	    		$row = $lockDao->registerLockInfo($data);
    		}

    		if($row !== false){
    			echo 1;
    		}else{
    			echo 0;
    		}
    	}
    }

    /**
     * 开锁记录
     **/
    public function openlog(){
    	$ajax = I('get.ajax', 0);
		$page = I('get.page', 1);
		//房间名称
		$doornumber = I('get.doornumber', '');
		if(!empty($doornumber)){
			$where['doornumber'] = $doornumber;
		}
		//获取分店
		$shop_id = I('get.shop_id', 0);
		if(!empty($shop_id)){
			$where['shop_id'] = $shop_id;
		}else{
			$where['shop_id'] = array('in', $this->shopidlist);
		}
		//获取小区条件
		$villageid = I('get.villageid', 0);
		if(!empty($villageid)){
			$where['villageid'] = $villageid;
		}
		//获取小区列表
		$villagelist = D('Village')->getVillageListByShopId(array(
			'shop_id' => array('in', $this->shopidlist)
		));
		$this->assign('villagelist', $villagelist);
		//获取总数
		$lockDao = D('Lock');
		$count = $lockDao->getLockOpenLogCount($where);
		$this->assign('count', $count);
		//获取列表
		$locklist = $lockDao->getLockOpenLogList($where, $page);
		$this->assign('locklist', $locklist);
		$this->assign('page', $page);
		if($ajax){
			$this->display('open_list', 'utf-8', 'html');
		}else{
			$this->display();
		}
    }

    /**
     * 指令记录
     **/
    public function cmdlog(){
    	$ajax = I('get.ajax', 0);
		$page = I('get.page', 1);
		//房间名称
		$doornumber = I('get.doornumber', '');
		if(!empty($doornumber)){
			$where['doornumber'] = $doornumber;
		}
		//获取分店
		$shop_id = I('get.shop_id', 0);
		if(!empty($shop_id)){
			$where['shop_id'] = $shop_id;
		}else{
			$where['shop_id'] = array('in', $this->shopidlist);
		}
		//获取小区条件
		$villageid = I('get.villageid', 0);
		if(!empty($villageid)){
			$where['villageid'] = $villageid;
		}
		//获取小区列表
		$villagelist = D('Village')->getVillageListByShopId(array(
			'shop_id' => array('in', $this->shopidlist)
		));
		$this->assign('villagelist', $villagelist);
		//获取总数
		$lockDao = D('Lock');
		$count = $lockDao->getLockCmdLogCount($where);
		$this->assign('count', $count);
		//获取列表
		$locklist = $lockDao->getLockCmdLogList($page,$where);
		$this->assign('locklist', $locklist);
		$this->assign('page', $page);
		if($ajax){
			$this->display('cmd_list', 'utf-8', 'html');
		}else{
			$this->display();
		}
    }

    /**
     * 门锁类型表
     **/
    public function locktype(){

    }
    
}